package com.ancun;

import ancun.seal.servlet.SealClient;
import ancun.seal.util.FileUtil;
import ancun.seal.util.StringUtil;
import org.junit.Test;

import java.io.File;
import java.util.UUID;

//import org.junit.*;

/**
 * PDF自动化签章
 *
 */
public class SealAutoCrossPdfTest extends ServletBaseTest{

    @Test
    public void SealAutoCrossPdfTest() throws Exception {
        /** HTTP方式 */
        //调用接口url拼接
        String url = super.url + SealClient.SLASH + SealClient.PDF_SEAL_SERVLET;
        SealClient sealClient = new SealClient(url);

        //参数赋值
        String sealCode = "cfca_3_1543453841356";//"cfca_2_1543457804359901";//"cfca_1_1543457804359901";szca_2_1565506978514221
        String sealPassword = "123456";//"ac520614";
//        String sealPerson = "";
//        String sealLocation = "";
//        String sealResson = "";
        String sealPerson = "江苏江南农村商业银行股份有限公司";
        String sealLocation = "江苏省常州市天宁区和平中路413 号";
        String sealResson = "江苏省常州市天宁区和平中路    413 号";

        String fromPage = "1";
        String toPage = "0";
        /**
         * 骑缝章风格
         * 1：上下各一半
         * 2：左右各一半
         * 3：上下骑缝
         * 4：左右骑缝
         * 5：左骑缝
         * 6：右骑缝
         */
        String crossStyle = "6";

//        String sealStrategyXML = "<Request>" + "<SealCode>" + sealCode
//                + "</SealCode><SealPassword>" + sealPassword + "</SealPassword><SealPerson>" + sealPerson
//                + "</SealPerson><SealLocation>" + sealLocation + "</SealLocation>" + "<SealResson>" + sealResson + "</SealResson>" +
//                "<FromPage>" + fromPage + "</FromPage>" + "<ToPage>" + toPage + "</ToPage>" +
//                "<CrossStyle>" + crossStyle + "</CrossStyle>" + "</Request>";

//        String sealStrategyXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<Request>\n" +
//        "<Head/>\n" +
//        "<Body>\n" +
//        "<SealCode>" + sealCode + "</SealCode>\n" +
//        "<SealPassword>" + sealPassword + "</SealPassword>\n" +
//        "<SealPerson>" + sealPerson + "</SealPerson>\n" +
//        "<SealResson>" + sealResson + "</SealResson>\n" +
//        "<FromPage>" + fromPage + "</FromPage>\n" +
//        "<ToPage>" + toPage + "</ToPage>\n" +
//        "<CrossStyle>" + crossStyle + "</CrossStyle>\n" +
//        "</Body>\n" + "</Request>";

        String sealStrategyXML = "<Request><Head/><Body>" + "<SealCode>" + sealCode
                + "</SealCode><SealPassword>" + sealPassword + "</SealPassword><SealPerson>" + sealPerson
                + "</SealPerson><SealLocation>" + sealLocation + "</SealLocation>" + "<SealResson>" + sealResson + "</SealResson>" +
                "<FromPage>" + fromPage + "</FromPage>" + "<ToPage>" + toPage + "</ToPage>" +
                "<CrossStyle>" + crossStyle + "</CrossStyle>" + "</Body></Request>";

        //指定生成的pdf模板路径
        String pdfURL = super.baseSources + "中原证券股份有限公司柜台市场客户协议dzhtgzc_zb.pdf";
//        String pdfURL = super.baseSources + "配送合作协议-podofo批量错误.pdf";
        byte[] pdf = FileUtil.getBytesFromFile(new File(pdfURL));

//        String pdfURL = "/home/hancm/tdbank-sealserver/4.pdf&/home/hancm/tdbank-sealserver/44.pdf";
//        byte[] pdf = pdfURL.getBytes();

        long time1 = System.currentTimeMillis();
        byte[] sealedPdf = null;
        sealedPdf = sealClient.sealAutoCrossPdf(pdf, sealStrategyXML);
        long time2 = System.currentTimeMillis();
        System.out.println("Time: " + (time2-time1));

        if (sealedPdf.length < 255) {
            System.out.println("签章路径: " + new String(sealedPdf));
        } else if (StringUtil.checkResultDataValid(sealedPdf)) {
            //指定生成PDF存储路径
            String savePath = super.targetSources + UUID.randomUUID() + ".pdf";
            FileUtil.wirteDataToFile(savePath, sealedPdf);
            System.out.println("com.ancun.SealAutoPdfTest(Servlet) ok and save data to \"" + savePath + "\'");
        } else {
            System.out.println("com.ancun.SealAutoPdfTest(Servlet) wrong " + new String(sealedPdf, "UTF-8"));
        }
        System.out.println(sealedPdf.length);
    }
}


